//
//  HBMySecurityCtrl.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/9.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBMySecurityCtrl.h"
#import "HBGetSecurityUrlInfo.h"
#import "HBGetSecurityUrlParam.h"
#import "HBGetSecurityUrlParser.h"

@interface HBMySecurityCtrl ()
<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *myWebView;
@property (nonatomic, strong) NSString *webUrl;

@end

@implementation HBMySecurityCtrl

#pragma mark - system

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.view.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
}

- (void)buildLayout
{
    [super buildLayout];
    self.title = @"安全保障";
    self.myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    self.myWebView.delegate = self;
    [self.view addSubview:self.myWebView];

    self.myWebView.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
    self.myWebView.opaque = NO;
   
    [self requestToGetUrl];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];

}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [self.myWebView stopLoading];
    self.myWebView.delegate = nil;
    [self.myWebView removeFromSuperview];
    self.myWebView = nil;
}

#pragma mark - delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    LLog(@"%@,%@",request.URL.absoluteString,self.webUrl);
    if ([request.URL.absoluteString isEqualToString:self.webUrl]||[request.URL.absoluteString isEqualToString:[NSString stringWithFormat:@"%@/",self.webUrl]])
    {
        [CustomLoadingView showHudToView:self.view];
    }
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [CustomLoadingView hideHudForView:self.view];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
     [CustomLoadingView hideHudForView:self.view];
}

#pragma mark - net

- (void)requestToGetUrl
{
    [CustomLoadingView showHudToView:self.view];
    HBGetSecurityUrlParam *param = [[HBGetSecurityUrlParam alloc]init];
    
    HBGetSecurityUrlParser *parser = [[HBGetSecurityUrlParser alloc]init];
    [NetCenter getDataFromURL:Method_getSecurityUrl params:param parser:parser result:^(ParserResult *myResult) {
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code==0)
        {
            HBGetSecurityUrlInfo *urlInfo = (HBGetSecurityUrlInfo *)myResult.result;
            self.webUrl = urlInfo.dataInfo;
            
            [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
}

@end
